package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2023-11-14
 * Time: 14:48
 */
public class AddOperation implements IOPeration{


    @Override
    public void work(Booklist booklist) {
        System.out.println("*******   新增图书:  *******");

        Scanner scanner = new Scanner(System.in);
        System.out.println("*******  请输入书名:  *******");
        String name =scanner.nextLine();

        System.out.println("*******  请输入作者:  *******");
        String author = scanner.nextLine();

        System.out.println("*******  请输入价格:  *******");
        int price = scanner.nextInt();

        System.out.println("*******  请输入类型:  *******");
        String  type = scanner.nextLine();

        Book book = new Book(name, author, price, type);

        //检查数组中有没有这本书
        int currentSize = booklist.getUsedsize();
        for (int i = 0; i < currentSize; i++) {
            if(book.equals(booklist.getbook(i))) {
                System.out.println("存在这本书,不需要添加");
                return;
            }
            if (currentSize == 10) {
                System.out.println("满了,不能进行添加");
                return;
            }else {
                booklist.setbook(currentSize,book);
                booklist.setUsedsize(currentSize++);
                System.out.println("添加成功!!");
            }
        }

    }
}
